const StockRequest = require("../../request/stock");
const PublicModel = require("../publicmodel");
const utils = require("../../utils");

module.exports = class StockModel extends PublicModel {

	constructor(_db, _id) {
		super(_db, "lg-stock-bases", _id);
		this._id = 0;
		this.symbol = ""; // 股票标识
		this.code = ""; // 编码
		this.name = ""; // 名称
		this.trade = 0; // 实时交易价
		this.price_change = 0; // 涨跌额
		this.change_percent = 0; // 涨跌幅
		this.buy = 0; // 买入价
		this.sell = 0; // 卖出价
		this.float_yclose = 0; // 昨日收盘价
		this.open = 0; // 开盘价
		this.high = 0; // 最高价
		this.low = 0; // 最低价
		this.volume = 0; // 成交量(手)
		this.amount = 0; // 成交额(元)
		this.tick_time = ""; // 数据实时获取时间
		this.pe = 0; // 市盈率
		this.pb = 0; // 市净率
		this.market_capitalization = ""; // 市值(元)
		this.turnover_rate = 0; // 换手率
		this.stock_type_code = ""; //股票类型代号
		this.stock_type_name = ""; //股票类型名称
		this.exchange_code = ""; //交易所代号
		this.exchange_name = ""; //交易所名称
		this.funds = []; // 基金列表
		this.create_date = ""; // 创建时间
		this.lastupdate_date = ""; // 最后更新时间
	}

	// 获取symbol数组
	async getSymbolList(init = false) {
		const {
			data: {
				0: {
					stock_list: stockList
				} = {}
			} = []
		} = await this._collection.aggregate().project({
			_id: false,
			symbol: 1
		}).group({
			_id: 1,
			stock_list: this._$.push("$symbol")
		}).end() || {};

		return init ? [] : stockList;
	}

	//根据symbol更新
	async updateBaseBySymbol(symbol, stock) {
		const {
			updated
		} = await this._collection.where({
			symbol
		}).update(stock) || {};
		return updated;
	}
	


}
